package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.utils.FocusUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class AutoSizeTextSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button, image1, image2;
    private Text text_title, text, point;
    private DependentLayout dependentLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_autoSizeText);
        initView();
        initZoom();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Auto size text")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Auto size text")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Auto size text", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Auto size text", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Auto size text", 1);
            pf.flushSync();
        }
    }

    private void initZoom() {
        dependentLayout.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.POINT_MOVE:
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        text.setHeight(FocusUtil.getFocus(touchEvent) - 180);
                        text.setAutoFontSize(true);
                        point.setText(String.valueOf(text.getTextSize()) + ".0sp");
                        point.setHeight(text.getTextSize() + 30);
                        point.setAutoFontSize(true);
                        break;
                }
                return false;
            }
        });
    }

    private void initView() {
        dependentLayout = (DependentLayout) findComponentById(ResourceTable.Id_dependentLayout);
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setClickedListener(this);
        text = (Text) findComponentById(ResourceTable.Id_text);
        text.setAutoFontSizeRule(30, 90, 1);
        point = (Text) findComponentById(ResourceTable.Id_point);
        point.setAutoFontSizeRule(30, 90, 1);

        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Auto size text");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        onBackPressed();
    }
}

